package org.nf.order.dao.impl;


import org.nf.db.util.ResultSetHandler;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;
import org.nf.db.util.result.ColumnHandler;
import org.nf.order.dao.OrderDao;
import org.nf.order.model.Order;
import org.nf.order.util.ConnUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.StreamHandler;

public class OrderDaoImpl implements OrderDao {


    @Override
    public int insertOrder(Order order) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "insert into design_buyinfo(u_id,product_id,product_name,product_price,location,buy_housetype,buy_style,buy_area,buy_fixPric,buy_prodctStatus) values(?,?,?,?,?,?,?,?,?,?)";
        return executor.executeUpdate(sql,order.getUId(),order.getProductName(), order.getProductPrice(),order.getLocation(),order.getBuyHouseTyte(),order.getBuyArea(),order.getBuyFixPric(),order.getProductStatus());
    }

    @Override
    public int deleteOrder(int productId) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "delete form design_buyinfo where product_id = ?";
        return executor.executeUpdate(sql,productId);
    }

    @Override
    public int updateOrder(Order order) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "update design_buyinfo set u_id=?,product_id=?,product_name=?,product_price=?,location=?,buy_housetype=?buy_style=?,buy_area=?,buy_date=?,buy_fixPric=?,buy_productStatus=? ";
        return executor.executeUpdate(sql,order.getUId(),order.getProductName(), order.getProductPrice(),order.getLocation(),order.getBuyHouseTyte(),order.getBuyArea(),order.getBuyFixPric(),order.getProductStatus(),order.getBuyDate());
    }

    @Override
    public List<Order> select(String uId) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<Order> handler = new BeanListHandler<>(Order.class);
        String sql = "select buy_id,u_id,product_id,product_name,product_price,location,buy_housetype,buy_style,buy_area,buy_date,buy_fixPric,buy_prodctStatus FROM design_buyinfo where u_id=?";
        return executor.executeQuery(sql,handler,uId);

    }

    @Override
    public List<Order> listOrder(int numberOf, int limit) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select buy_id,u_id,product_id,product_name,product_price,location,buy_housetype,buy_style,buy_area,buy_date,buy_fixPric,buy_prodctStatus FROM design_buyinfo limit ?,?";
        BeanListHandler<Order> handler = new BeanListHandler<>(Order.class);

        return executor.executeQuery(sql,handler,limit);
    }

    @Override
    public long orderCount() {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select count(*) FROM design_buyinfo";
        ColumnHandler<Long> handler = new ColumnHandler<>(1);
        return executor.executeQuery(sql,handler);
    }


}